<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="培养单位">
              <a-select :disabled="selectMenu.yxList.length === 1" allowClear v-model="queryParam.kcyx" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="开课院系">
              <a-select allowClear v-model="queryParam.kcdw" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxAllList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="课程名称">
              <a-select allowClear v-model="queryParam.kcdm" showSearch :filterOption="filterOption">
                <a-select-option v-for="(i,index) in selectMenu.courseList" :key="index" :value="i.kch">{{i.kch}}-{{ i.kcmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="修课状态">
              <a-select allowClear v-model="queryParam.xkzt" showSearch>
                <!-- <a-select-option v-for="i in TYPE0326" :key="i.id" :value="i.code">{{ i.name }}</a-select-option> -->
                <a-select-option value="01">未修</a-select-option>
                <a-select-option value="02">正在修课</a-select-option>
                <a-select-option value="03">已经修课</a-select-option>
                <a-select-option value="04">重修</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="考试年级"
              >
                <a-select allowClear v-model="queryParam.ksnj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="考试学年"
              >
                <a-select allowClear v-model="queryParam.ksxn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试学期"
              >
                <a-select allowClear v-model="queryParam.ksxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试性质"
              >
                <a-select allowClear v-model="queryParam.ksxz">
                  <a-select-option v-for="i in TYPE0358" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试类别"
              >
                <a-select allowClear v-model="queryParam.kslb">
                  <a-select-option v-for="i in TYPE0109" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="成绩类型"
              >
                <a-select allowClear v-model="queryParam.cjlxm">
                  <a-select-option v-for="i in TYPE0328" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="成绩状态"
              >
                <a-select allowClear v-model="queryParam.cjzt">
                  <a-select-option v-for="i in TYPE0127" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="学号"
              >
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="姓名"
              >
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学籍状态">
                <a-select allowClear v-model="queryParam.xjztm" placeholder="请选择">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="8" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" style="margin:0 10px" @click="handleExport">导出</a-button>
            <a-button @click="queryParam = {};$refs.table.refresh()">重置</a-button>

            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :scroll="{ x: 1500 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <a
          slot="option"
          slot-scope="text, record"
          v-hasPermission="'kccjcx:modify'"
          v-if="record.cjlxm !== '03' && record.cjzt === '07'"
          @click="showModify(record)">修改</a>

      </s-table>
    </a-card>

    <modify-score
      :visible="scoreVisible"
      ref="score"
      @refresh="$refs.table.refresh(true)"
      @cancel="scoreVisible = false"
      :xs="xsScore"/>

  </div>
</template>

<script>
  import {STable} from '~'
  import {mapState} from 'vuex'
  import modifyScore from './modifyScore'

  export default {
  data () {
    return {
      advanced: true,
      queryParam: {
        xjztm: ''
      },
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '学号',
          dataIndex: 'xsxh',
          width:150
        },
        {
          title: '姓名',
          dataIndex: 'xm',
          width:100
        },
        {
          title: '年级',
          dataIndex: 'ksnjmc',
          width:70
        },
        {
          title: '课程代码',
          dataIndex: 'kcbm',
          width:120
        },
        {
          title: '修课状态',
          dataIndex: 'xkztmc',
          width:100
        },
        {
          title: '考试学年',
          dataIndex: 'ksxn',
          width:120
        },
        {
          title: '学期',
          dataIndex: 'ksxqmc',
          width:100
        },
        {
          title: '课程名称',
          dataIndex: 'kcmc'
        },
        {
          title: '成绩',
          dataIndex: 'kscj'
        },
        {
          title: '成绩类型',
          dataIndex: 'cjlxmc'
        },
        {
          title: '成绩状态',
          dataIndex: 'cjztmc'
        },
        {
          title: '备注',
          dataIndex: 'note'
        },
        {
          title: '操作',
          fixed: 'right',
          width: 100,
          scopedSlots: { customRender: 'option' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.scoreStuList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList: [],
        yxAllList: []
      },
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false,
      scoreVisible: false,
      xsScore: {}
    }
  },
  components: {STable, modifyScore},
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0127: state => state.app['TYPE0127'],
      TYPE0072: state => state.app['TYPE0072'],
      TYPE0109: state => state.app['TYPE0109'],
      TYPE0358: state => state.app['TYPE0358'],
      TYPE0328: state => state.app['TYPE0328'],
      TYPE0326: state => state.app['TYPE0326'],
      TYPE0073: state => state.app['TYPE0073'],

    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0127')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0109')
    this.$store.dispatch('app/setDictionary', 'TYPE0358')
    this.$store.dispatch('app/setDictionary', 'TYPE0328')
    // this.$store.dispatch('app/setDictionary', 'TYPE0326')
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo'),
        this.$api.base.collegeNoAuth()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data,
          yxAllList: res[2].data
        }
        this.$set(this.queryParam, 'ksnj', res[1].currentGrade)
        this.$set(this.queryParam, 'ksxq', res[1].currentSemester)
        this.$set(this.queryParam, 'ksxn', res[1].schoolYear)
        // this.queryParam.kcyx=res[0].data[0].dwh;
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'kcyx', this.selectMenu.yxList[0].dwh)
          this.yxChange(this.queryParam.kcyx)
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    handleExport(){
      this.$export('foster/kccj/xsListExport',this.queryParam)
    },
    yxChange (code) {
      this.queryParam.kcbm = ''
      this.$set(this.selectMenu, 'courseList', [])
      if (code) {
        this.kcLoading = true
        this.$api.cultivate.selectList4AddCourse({kcssyxm: code}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    showModify (xs) {
      this.xsScore = xs
      this.scoreVisible = true
      this.$refs.score.kscj = xs.kscj
      this.$refs.score.note = xs.note
    }
  }
}
</script>

<style>

</style>
